package net.huashitong.user.dao;

import java.util.List;


import com.sirdc.modules.utils.StringUtils;
import net.huashitong.user.entity.Collection;
import net.huashitong.user.filter.CollectionFilter;

import org.springframework.stereotype.Repository;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;

@Repository
public class CollectionDao extends StringDao<Collection> {

	public List<Collection> queryByUserId(CollectionFilter filter) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.userId = :userId").setParam("userId", filter.getUserId());
		query.append(" order by obj.date DESC");
		return query.listResult(filter);
	}
    public Collection getByUserIdAndAlertId(CollectionFilter filter) {
		GenericQuery query = createQuery("obj");
		if(StringUtils.isNotBlank(filter.getUserId())){
			query.append(" and obj.userId = :userId").setParam("userId", filter.getUserId());
		}
		if(StringUtils.isNotBlank(filter.getAlertId())){
			query.append(" and obj.alertId = :alertId").setParam("alertId", filter.getAlertId());
		}
		if(StringUtils.isNotBlank(filter.getProjectId())){
			query.append(" and obj.projectId = :projectId").setParam("projectId", filter.getProjectId());
		}
		return query.singleResult();
	}

}
